package com.smec.remes.module.submit.adapter;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.smec.moblie.BR;
import com.smec.moblie.R;
import com.smec.moblie.databinding.LayoutRecyclerviewUnfinishBinding;
import com.smec.remes.base.SmecBaseRecyclerViewHolder;
import com.smec.remes.bean.SubmitFinish;
import com.smec.remes.module.submit.activity.SmecLogActivity;
import com.smec.remes.module.submit.activity.SmecSubmitDetailsActivity;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/19.
 */

public class SmecSubmitAdapter  extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {

    private Context mcontext;
    private String  _fragment;
    private ArrayList<SubmitFinish> submitFinishArrayList=new ArrayList<>();

    public SmecSubmitAdapter(Context context) {
        this.mcontext=context;
    }

    public void setArrayList(ArrayList<SubmitFinish> list,String status,String form){
        if(status.equals("onRefresh")){
            submitFinishArrayList=list;
        }else {
            submitFinishArrayList.addAll(list);
        }
        _fragment=form;
        notifyDataSetChanged();
    }

    public void clearList(){
        submitFinishArrayList.clear();
        notifyDataSetChanged();
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutRecyclerviewUnfinishBinding layoutRecyclerviewUnfinishBinding=DataBindingUtil.inflate
                (LayoutInflater.from(parent.getContext()), R.layout.layout_recyclerview_unfinish,parent,false);
        SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                SmecBaseRecyclerViewHolder(layoutRecyclerviewUnfinishBinding.getRoot());
        smecBaseRecyclerViewHolder.setViewType(viewType);
        smecBaseRecyclerViewHolder.setBinding(layoutRecyclerviewUnfinishBinding);
        return smecBaseRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, final int position) {
        final LayoutRecyclerviewUnfinishBinding layoutRecyclerviewUnfinishBinding= (LayoutRecyclerviewUnfinishBinding) holder.getBinding();
        layoutRecyclerviewUnfinishBinding.setVariable(BR.submit,submitFinishArrayList.get(position));
        layoutRecyclerviewUnfinishBinding.llsDrafts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mcontext, SmecSubmitDetailsActivity.class);
                intent.putExtra("name",_null(layoutRecyclerviewUnfinishBinding.tvUnName.getText().toString()));
                intent.putExtra("use",_null(submitFinishArrayList.get(position).getPurposeName()));
                intent.putExtra("date",_null(layoutRecyclerviewUnfinishBinding.tvUnDate.getText().toString()));
                intent.putExtra("applyOrderNo",submitFinishArrayList.get(position).getApplyOrderNo().toString());
                intent.putExtra("node",submitFinishArrayList.get(position).getNode().toString());
                intent.putExtra("remark",submitFinishArrayList.get(position).getRemark() == null ? " ":submitFinishArrayList.get(position).getRemark().toString());
                intent.putExtra("_fragment",_fragment);
                if(submitFinishArrayList.get(position).getAllocOrderNo()!=null && !submitFinishArrayList.get(position).getAllocOrderNo().equals("")){
                    intent.putExtra("dbName",submitFinishArrayList.get(position).getAllocOrderNo().toString());
                }
                mcontext.startActivity(intent);
            }
        });

        layoutRecyclerviewUnfinishBinding.logDirect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mcontext, SmecLogActivity.class);
                intent.putExtra("applyOrderNo",submitFinishArrayList.get(position).getApplyOrderNo().toString());
                mcontext.startActivity(intent);
            }
        });

        if(submitFinishArrayList.get(position).getNode().equals("维保经理审批中")|| submitFinishArrayList.get(position).getNode().equals("维保站长校对中")){
            layoutRecyclerviewUnfinishBinding.llNode.setBackground(mcontext.getResources().getDrawable(R.drawable.status_weibao));
        }else if(submitFinishArrayList.get(position).getNode().equals("待分配")){
            layoutRecyclerviewUnfinishBinding.llNode.setBackground(mcontext.getResources().getDrawable(R.drawable.status_daifen));
        }else if(submitFinishArrayList.get(position).getNode().equals("待收货")){
            layoutRecyclerviewUnfinishBinding.llNode.setBackground(mcontext.getResources().getDrawable(R.drawable.status_daishou));
        }else if(submitFinishArrayList.get(position).getNode().equals("已完成")){
            layoutRecyclerviewUnfinishBinding.llNode.setBackground(mcontext.getResources().getDrawable(R.drawable.status_wancheng));
        }else if(submitFinishArrayList.get(position).getNode().equals("调拨单待出库")){
            layoutRecyclerviewUnfinishBinding.llNode.setBackground(mcontext.getResources().getDrawable(R.drawable.status_db));
        }else {
            layoutRecyclerviewUnfinishBinding.llNode.setBackground(mcontext.getResources().getDrawable(R.drawable.status_bohui));
        }
    }

    public String _null(String a){
        if(TextUtils.isEmpty(a)){
            a="";
        }
        return a;
    }

    @Override
    public int getItemCount() {
        return submitFinishArrayList.size();
    }
}
